package com.xsoft.sms.cmpp20.comm.handlers;

import com.xsoft.sms.cmpp20.message.impl.CMPP20Message;
import org.apache.log4j.Logger;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;

/**
 * package: {PACKAGE_NAME}
 * User: zhanglm
 * Date: 12-9-18
 * Time: 下午2:48
 * Description:
 */
public class CMPP20MessageEncoder extends OneToOneEncoder {

	private static Logger logger = Logger.getLogger(CMPP20MessageEncoder.class);

	@Override
	protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
		if (!(msg instanceof CMPP20Message)) {
			return ChannelBuffers.EMPTY_BUFFER;
		}

		CMPP20Message cmpp20Msg = (CMPP20Message) msg;
		byte[] data = cmpp20Msg.toBuffer();

		ChannelBuffer channelBuffer = ChannelBuffers.buffer(data.length);
		channelBuffer.writeBytes(data);
		return channelBuffer;
	}
}
